package com.example.administrator.zyfypt_422zwj.fragment;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;

import com.example.administrator.zyfypt_422zwj.R;
import com.example.administrator.zyfypt_422zwj.activity.CMainActivity;
import com.example.administrator.zyfypt_422zwj.activity.FocusActivity;

import static android.content.Context.MODE_PRIVATE;


/**
 * Created by Neusoft on 2017/4/9.
 */

public class Fragment6 extends Fragment {
    private TextView tvinfo,tvuser;
    private View view=null;
    private Context context;

    private Button b1,b2,b3;

    private SharedPreferences sp;
    private String username;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        context=getActivity();
       //动态加载Fragment6的布局文件
        view=inflater.inflate(R.layout.fragment6,container,false);
        //获取布局上的控件，注意通过view获取
        tvinfo=(TextView)view.findViewById(R.id.textView8);
        tvuser=(TextView)view.findViewById(R.id.textView16);

        b1=(Button)view.findViewById(R.id.button9);
        b2=(Button)view.findViewById(R.id.button6);
        b3=(Button)view.findViewById(R.id.button7);

        tvinfo.setText("我的资源");
        b1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switch (v.getId()){
                    case R.id.button9:
                        getActivity().onBackPressed();
                        break;
                }
            }
        });

        b2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switch (v.getId()){
                    case R.id.button6:
                        System.out.println("点击我的关注按钮！");
                        Intent intent = new Intent();
                        intent.setClass(getActivity(),FocusActivity.class);
                        startActivity(intent);
                        break;
                }
            }
        });

        b3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switch (v.getId()){
                    case R.id.button7:
                        System.out.println("点击我的收藏按钮！");
                        Intent intent = new Intent();
                        intent.setClass(getActivity(),CMainActivity.class);
                        startActivity(intent);
                        break;
                }
            }
        });

        //实例化sp
        sp=context.getSharedPreferences("login",MODE_PRIVATE);
        //读取sp
        readsp();
        tvuser.setText(username);

        //注意Toast第一个参数上下文
        Toast.makeText(context, "加载我的资源", Toast.LENGTH_SHORT).show();
        //返回动态生成的view
        return view;
    }

    private void readsp() {
        username = sp.getString("username", null);
    }
}
